﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Text.RegularExpressions;
namespace Demo_HHT_EARTH
{
    public partial class EQcheckin : Form
    {
        //
        SQLiteConnection cn2 = new SQLiteConnection();
        DataSet Lds = new DataSet();
        DataTable Table_Eq = new DataTable();
        string streqcode = string.Empty;
        public EQcheckin(string cklab)
        {
            InitializeComponent();
            if (cklab == "success")
            {
               label1.Visible = true;
                label1.Text = "Check In Equipment Successful";
                createDatagrid();
                textBox1.Focus(); 
            }
            else
            {
                label1.Visible = false;
                createDatagrid();
                textBox1.Focus(); 
            }
            
           
            
            
        }

        private void createDatagrid()
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                MessageBox.Show("notconect SQLite");
            }
            string strcomm = "SELECT * FROM t_earth_flight_equipment";   //FlightEquipment
            SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
            SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
            Lds = new DataSet();
            cn2.Open();
            Lda.Fill(Lds, "L_emp");
            cn2.Close();
            dataGrid1.DataSource = null;
            if (Lds == null)
            {
                MessageBox.Show("No have data SQLite");
            }
            else
            {
                Table_Eq = CreateTable_Emp(Lds);
                
                dataGrid1.DataSource = Table_Eq;




            }

          //  dataGrid1.DataSource = Lds.Tables[0];
           
        }

        private DataTable CreateTable_Emp(DataSet Lds)
        {
            Table_Eq = new DataTable();
            Table_Eq.Columns.Add("Equipment Code");
            Table_Eq.Columns.Add("In");
           

            for (int i = 0; i < Lds.Tables[0].Rows.Count; i++)
            {
                string eq_id = Lds.Tables[0].Rows[i]["equipment_code"].ToString();

                string eq_ck = Lds.Tables[0].Rows[i]["check_in_flag"].ToString();
                
                if (eq_ck.Trim() == "YES")
                {
                    eq_ck = "   X   ";
                    // emp_ck = String.Format("{5,5}",emp_ck).ToString();//emp_ck = StringAlignment.Center;
                }
                else if (eq_ck.Trim() == "NO")
                {
                    eq_ck = "";
                }

                Table_Eq.Rows.Add(eq_id, eq_ck);
            }

         

            return Table_Eq;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
            DEMO_TEMP1.MainMenu Mn = new DEMO_TEMP1.MainMenu(true, 2);
            Mn.Show();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //this.Close();
            //DEMO_TEMP1.MainMenu Mn = new DEMO_TEMP1.MainMenu(true, 2);
            //Mn.Show();
            textBox1.Focus();
            createDatagrid();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
            //if (e.KeyChar.ToString() != "\r")
            //{
            //    streqcode += e.KeyChar.ToString();
            //}
            if (e.KeyChar.ToString() == "\r")
            {
                WorkScan();
                streqcode = "";
            }
            else if (e.KeyChar.ToString() == "\b")
            {
                textBox1.Text = String.Empty;
            }
        }

        private void WorkScan()
        {
            streqcode = textBox1.Text;
            bool ck = VaridateEq_fomat(streqcode);
            int check_in = ValidatedEq_Code(streqcode);
            if (ck)
            {
                Demo_HHT_EARTH.DataGetEqCheckIn.eq_code = streqcode;
                if (check_in==1)
                {
                    UpdateEq_code(streqcode);
                    label1.Visible = true;
                    label1.Text = "Check In Equipment Successful";
                    textBox1.Focus();    // update
                }
                else if (check_in==-1)
               
                {
                    Demo_HHT_EARTH.CheckInConfirmTemp ConfTemp = new CheckInConfirmTemp(1);
                    ConfTemp.Show();   // Goshow
                    //this.Close();

                }
                else if (check_in == 0)
                {
                    label1.Visible = true;
                    label1.Text = "Check In Equipment Duplicate";
                    textBox1.Focus();
                }
                textBox1.Text = ""; 
            }
            else
            {
                label1.Visible = true;
                label1.Text = "Equipment Code Error";
            }
            

        }

        private void UpdateEq_code(string streqcode)
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                MessageBox.Show("notconect SQLite");
            }

            string strcomm = "Update t_earth_flight_equipment Set check_in_flag = 'YES' where equipment_code = @streqcode";
            SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
            COMM.Parameters.AddWithValue("@streqcode", streqcode);
            SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
            cn2.Open();
            COMM.ExecuteNonQuery();

            cn2.Close();
            createDatagrid();
        }

        private int ValidatedEq_Code(string streqcode)
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                MessageBox.Show("notconect SQLite");
            }
            string strcomm = "SELECT * FROM t_earth_flight_equipment where equipment_code = @streqcode";
            SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
            COMM.Parameters.AddWithValue("@streqcode", streqcode);
            SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
            DataSet testT = new DataSet();
            cn2.Open();
            Lda.Fill(testT, "B");
            cn2.Close();
            if (testT.Tables[0].Rows.Count > 0)
            {
                if (testT.Tables[0].Rows[0]["check_in_flag"].ToString()=="NO")
                {
                    dataGrid1.DataSource = null;
                    dataGrid1.DataSource = testT.Tables[0];
                    return 1;      // update

                }
                else
                {
                    return 0;   // duplicate
                }
               
            }

            else
            {
                return -1;   // Goshow
            }
        }

        private bool VaridateEq_fomat(string emc)
        {
            //นำค่ามาลงในตัวแปร Data
           string Data = emc;
            //กำหนดรูปแบบที่รับเข้ามาตามต้องการ"^([])"
            string pattern = "^([0-9a-zA-Z]*[0-9a-zA-Z]*[0-9a-zA-Z])*-([0-9a-zA-Z]*[0-9a-zA-Z]*[0-9a-zA-Z]*[0-9a-zA-Z])$";
            if (Regex.IsMatch(Data, pattern))
            {

                return true;
            }
            else
            {
                return false;

            }

        }
    }
}